package com.LeeCode;

/**
 * 尽可能使字符串相等
 */

public class Code1208 {
    public static void main(String[] args) {
        String s = "abcd";
        String t = "cdef";
        int maxCost = 3;

        System.out.println(new Code1208().equalSubstring(s, t, maxCost));
    }

    public int equalSubstring(String s, String t, int maxCost) {
        int ans = 0, left = 0, n = s.length();
        int cost = 0;

        char[] sc = s.toCharArray();
        char[] tc = t.toCharArray();

        for (int i = 0; i < n; i++) {
            cost += Math.abs(sc[i] - tc[i]);

            while (cost > maxCost) {
                cost -= Math.abs(sc[left] - tc[left]);
                left++;
            }
            ans = Math.max(ans, i - left + 1);
        }
        return ans;
    }
}
